package com.example.zkweixiu.app.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.InputType;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.zkweixiu.app.R;
import com.example.zkweixiu.app.bean.ObjBean;
import com.example.zkweixiu.app.pro.PublicPro;
import com.google.gson.Gson;
import com.vondear.rxtool.RxActivityTool;
import com.vondear.rxtool.RxRegTool;
import com.yanzhenjie.sofia.Sofia;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

public class ChangeNumber3Activity extends AppCompatActivity implements View.OnClickListener {
    private Intent intent;
    private RelativeLayout rl_change;
    private EditText ed_phone;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_number3);
        Sofia.with(ChangeNumber3Activity.this).statusBarDarkFont().statusBarBackground(Color.parseColor("#FFFFFF"));

        rl_change  = findViewById(R.id.rl_change);
        ImageView iv_fanhui = findViewById(R.id.iv_fanhui);
        View iv_fanhui2 = findViewById(R.id.iv_fanhui2);
        ed_phone = findViewById(R.id.ed_phone);

        iv_fanhui.setOnClickListener(this);
        iv_fanhui2.setOnClickListener(this);
        rl_change.setOnClickListener(this); //下一步
        ed_phone.setRawInputType(InputType.TYPE_CLASS_NUMBER); //首选输入类型为数字

        RxActivityTool.addActivity(ChangeNumber3Activity.this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.rl_change:
/*                if (true){
                    intent = new Intent(ChangeNumber3Activity.this, ChangeNumber4Activity.class);// 账号修改 - 验证新手机号 验证码
                    intent.putExtra("phone","17734586066");
                    startActivity(intent);
                    return;
                }*/


                String phone2 = ed_phone.getText().toString().trim();
                
                if (TextUtils.isEmpty(phone2)){
                    Toast.makeText(this, "请输入手机号", Toast.LENGTH_SHORT).show();
                    return;
                }

                if (!RxRegTool.isMobile(phone2)) {
                    Toast.makeText(this, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
                    return;
                }

                rl_change.setEnabled(false);
                sendSIM(phone2);


                break;
            case R.id.iv_fanhui:
                finish();
                break;
            case R.id.iv_fanhui2:
                finish();
                break;

        }
    }


    @Override
    protected void onStart() {
        super.onStart();
        rl_change.setEnabled(true);
    }

    /**
     * 发送验证码
     * */
    private void sendSIM(final String phone2) {

        OkHttpUtils
                .get()
                .url(PublicPro.PUT_SIM)
                .addParams("phone", phone2)
                .build()
                .execute(new StringCallback() {


                    @Override
                    public void onError(okhttp3.Call call, Exception e, int id) {
                        Log.e("HomeFragment1", e.getMessage());
                        rl_change.setEnabled(true);
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.e("HomeFragment1", response);


                        ObjBean objBean = new Gson().fromJson(response, ObjBean.class);

                        if (objBean.getStatus() == 200) {
                           // handler.sendEmptyMessage(200);
                            Toast.makeText(ChangeNumber3Activity.this, "发送成功", Toast.LENGTH_SHORT).show();
                            intent = new Intent(ChangeNumber3Activity.this, ChangeNumber4Activity.class);// 账号修改 - 验证新手机号 验证码
                            intent.putExtra("phone",phone2);
                            startActivity(intent);
                        } else {
                            rl_change.setEnabled(true);
                            Toast.makeText(ChangeNumber3Activity.this, objBean.getMsg(), Toast.LENGTH_SHORT).show();
                        }


                    }
                });
    }

        
}
